package cn.iocoder.yudao.module.system.controller.admin.portal;

import cn.iocoder.yudao.module.system.controller.admin.portal.vo.PortalDeptPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.portal.vo.PortalDeptRespVO;
import cn.iocoder.yudao.module.system.controller.admin.portal.vo.PortalDeptSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.portal.PortalDeptDO;
import cn.iocoder.yudao.module.system.service.portal.PortalDeptService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 门户网站用户部门")
@RestController
@RequestMapping("/portalDept")
@Validated
public class PortalDeptController {

    @Resource
    private PortalDeptService deptService;

    @PostMapping("/create")
    @Operation(summary = "创建门户网站用户部门")
    public CommonResult<String> createDept(@Valid @RequestBody PortalDeptSaveReqVO createReqVO) {
        return success(deptService.createDept(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门户网站用户部门")
    public CommonResult<Boolean> updateDept(@Valid @RequestBody PortalDeptSaveReqVO updateReqVO) {
        deptService.updateDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门户网站用户部门")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDept(@RequestParam("id") String id) {
        deptService.deleteDept(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除门户网站用户部门")
    public CommonResult<Boolean> deleteDeptList(@RequestParam("ids") List<String> ids) {
        deptService.deleteDeptListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得门户网站用户部门")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<PortalDeptRespVO> getDept(@RequestParam("id") String id) {
        PortalDeptDO dept = deptService.getDept(id);
        return success(BeanUtils.toBean(dept, PortalDeptRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门户网站用户部门分页")
    public CommonResult<List<PortalDeptRespVO>> getDeptPage(@Valid PortalDeptPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PortalDeptDO> pageResult = deptService.getDeptPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, PortalDeptRespVO.class));
    }

    /**
     * 获取部门全部list集合
     */
    @GetMapping("/get-all-list")
    @Operation(summary = "获取部门全部list集合")
    public CommonResult<List<PortalDeptRespVO>> getAllDeptList() {
        return success(BeanUtils.toBean(deptService.getDeptList(), PortalDeptRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门户网站用户部门 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeptExcel(@Valid PortalDeptPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PortalDeptDO> list = deptService.getDeptPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门户网站用户部门.xls", "数据", PortalDeptRespVO.class,
                        BeanUtils.toBean(list, PortalDeptRespVO.class));
    }

}